﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections;

namespace WaterLeverMonitor
{
    public partial class XunHuan : Form
    {
        private String u;
        private Form1 form1;
        private SerialPort serilport;
        public XunHuan(Form1 f)
        {
            InitializeComponent();
            form1 = f;
            serilport = form1.SerialPort;
            
        }
        //private void SendBytesData(SerialPort serialPort)
        //{
        //    string[] strSend = txtSend.Text.Trim().Split();
        //    byte[] bytesSend = new byte[strSend.Length];
        //    for (int i = 0; i < strSend.Length; i++)
        //    {
        //        bytesSend[i] = (byte)Convert.ToInt32(strSend[i], 16);

        //    }
        //    serialPort.Write(bytesSend, 0, bytesSend.Length);
        //}
        private byte[] HexStringToByteArray(string s)
        {


            s = s.Replace(" ", "");//把字符串内的空格去掉

            byte[] buffer = new byte[s.Length / 2];//定义新的byte数组，存放转换后的字符


            for (int i = 0; i < s.Length; i += 2)
                buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);



            return buffer;

        }
        ArrayList arX1 = new ArrayList();
        ArrayList arY1 = new ArrayList();

        ArrayList arX2 = new ArrayList();
        ArrayList arY2 = new ArrayList();

        ArrayList arX3 = new ArrayList();
        ArrayList arY3 = new ArrayList();

        ArrayList arX4 = new ArrayList();
        ArrayList arY4 = new ArrayList();

        ArrayList arX5 = new ArrayList();
        ArrayList arY5 = new ArrayList();

        ArrayList arX6 = new ArrayList();
        ArrayList arY6 = new ArrayList();

        ArrayList arX7 = new ArrayList();
        ArrayList arY7 = new ArrayList();

        ArrayList arX8 = new ArrayList();
        ArrayList arY8 = new ArrayList();

        ArrayList arX9 = new ArrayList();
        ArrayList arY9 = new ArrayList();

        ArrayList arX10 = new ArrayList();
        ArrayList arY10 = new ArrayList();
        public bool cb1 = false;
        public bool cb2 = false;
        public bool cb3 = false;
        public bool cb4 = false;
        public bool cb5 = false;
        public bool cb6 = false;
        public bool cb7 = false;
        public bool cb8 = false;
        public bool cb9 = false;
        public bool cb10 = false;

        private void button1_Click(object sender, EventArgs e)
        {

            if (checkBox11.Checked == true)
            {
                cb1 = true;
                arX1.Clear();
                arY1.Clear();
                //form1.timer1.Interval = Convert.ToInt32(textBox11.Text);


                string[] x = textBox1.Text.Trim().Split();
                string[] y = textBox21.Text.Trim().Split();
                int sizex = x.Length;
                int sizey = y.Length;
                string[] pointVx = new string[sizex];
                string[] pointVy = new string[sizey];
                //float[] pointVy = new float[size];
                for (int i = 0; i < sizex; i++)
                {
                    pointVx[i] = x[i].ToString();
                }
                for (int i = 0; i < sizey; i++)
                {
                    pointVy[i] = y[i].ToString();
                }
                for (int i = 0; i < sizex; i++)
                {
                    arX1.Add(pointVx[i]);
                }
                for (int i = 0; i < sizey; i++)
                {
                    arY1.Add(pointVy[i]);

                }
                form1.XAxis1 = arX1;
                if (checkBox1.Checked == true)
                    form1.YAxis1 = arY1;

                else
                {
                    arY1.Clear();
                    form1.YAxis1 = arY1;
                }
                
                form1.Time1();

            }
            else
                cb1 = false;
            if (checkBox12.Checked == true)
            {
                cb2 = true;
                arX2.Clear();
                arY2.Clear();

                string[] x = textBox2.Text.Trim().Split();
                string[] y = textBox22.Text.Trim().Split();
                int sizex = x.Length;
                int sizey = y.Length;
                string[] pointVx = new string[sizex];
                string[] pointVy = new string[sizey];
                //float[] pointVy = new float[size];
                for (int i = 0; i < sizex; i++)
                {
                    pointVx[i] = x[i].ToString();
                }
                for (int i = 0; i < sizey; i++)
                {
                    pointVy[i] = y[i].ToString();
                }
                for (int i = 0; i < sizex; i++)
                {
                    arX2.Add(pointVx[i]);
                }
                for (int i = 0; i < sizey; i++)
                {
                    arY2.Add(pointVy[i]);

                }
                form1.XAxis2 = arX2;
                if (checkBox2.Checked == true)
                    form1.YAxis2 = arY2;

                else
                {
                    arY2.Clear();
                    form1.YAxis2 = arY2;
                }
                

            }
            else
                cb2 = false;
            if (checkBox13.Checked == true)
            {
                cb3 = true;
                arX3.Clear();
                arY3.Clear();

                string[] x = textBox3.Text.Trim().Split();
                string[] y = textBox23.Text.Trim().Split();
                int sizex = x.Length;
                int sizey = y.Length;
                string[] pointVx = new string[sizex];
                string[] pointVy = new string[sizey];
                //float[] pointVy = new float[size];
                for (int i = 0; i < sizex; i++)
                {
                    pointVx[i] = x[i].ToString();
                }
                for (int i = 0; i < sizey; i++)
                {
                    pointVy[i] = y[i].ToString();
                }
                for (int i = 0; i < sizex; i++)
                {
                    arX3.Add(pointVx[i]);
                }
                for (int i = 0; i < sizey; i++)
                {
                    arY3.Add(pointVy[i]);

                }
                form1.XAxis3 = arX3;
                if (checkBox3.Checked == true)
                    form1.YAxis3 = arY3;

                else
                {
                    arY3.Clear();
                    form1.YAxis3 = arY3;
                }
                

            }
            else
                cb3 = false;
            if (checkBox14.Checked == true)
            {
                cb4 = true;
                arX4.Clear();
                arY4.Clear();

                string[] x = textBox4.Text.Trim().Split();
                string[] y = textBox24.Text.Trim().Split();
                int sizex = x.Length;
                int sizey = y.Length;
                string[] pointVx = new string[sizex];
                string[] pointVy = new string[sizey];
                //float[] pointVy = new float[size];
                for (int i = 0; i < sizex; i++)
                {
                    pointVx[i] = x[i].ToString();
                }
                for (int i = 0; i < sizey; i++)
                {
                    pointVy[i] = y[i].ToString();
                }
                for (int i = 0; i < sizex; i++)
                {
                    arX4.Add(pointVx[i]);
                }
                for (int i = 0; i < sizey; i++)
                {
                    arY4.Add(pointVy[i]);

                }
                form1.XAxis4 = arX4;
                if (checkBox4.Checked == true)
                    form1.YAxis4 = arY4;

                else
                {
                    arY4.Clear();
                    form1.YAxis4 = arY4;
                }
        

            }
            else
                cb4 = false;
            if (checkBox15.Checked == true)
            {
                cb5 = true;
                arX5.Clear();
                arY5.Clear();

                string[] x = textBox5.Text.Trim().Split();
                string[] y = textBox25.Text.Trim().Split();
                int sizex = x.Length;
                int sizey = y.Length;
                string[] pointVx = new string[sizex];
                string[] pointVy = new string[sizey];
                //float[] pointVy = new float[size];
                for (int i = 0; i < sizex; i++)
                {
                    pointVx[i] = x[i].ToString();
                }
                for (int i = 0; i < sizey; i++)
                {
                    pointVy[i] = y[i].ToString();
                }
                for (int i = 0; i < sizex; i++)
                {
                    arX5.Add(pointVx[i]);
                }
                for (int i = 0; i < sizey; i++)
                {
                    arY5.Add(pointVy[i]);

                }
                form1.XAxis5 = arX5;
                if (checkBox5.Checked == true)
                    form1.YAxis5 = arY5;

                else
                {
                    arY5.Clear();
                    form1.YAxis5 = arY5;
                }
            

            }
            else
                cb5 = false;
            if (checkBox16.Checked == true)
            {
                cb6 = true;
                arX6.Clear();
                arY6.Clear();

                string[] x = textBox6.Text.Trim().Split();
                string[] y = textBox26.Text.Trim().Split();
                int sizex = x.Length;
                int sizey = y.Length;
                string[] pointVx = new string[sizex];
                string[] pointVy = new string[sizey];
                //float[] pointVy = new float[size];
                for (int i = 0; i < sizex; i++)
                {
                    pointVx[i] = x[i].ToString();
                }
                for (int i = 0; i < sizey; i++)
                {
                    pointVy[i] = y[i].ToString();
                }
                for (int i = 0; i < sizex; i++)
                {
                    arX6.Add(pointVx[i]);
                }
                for (int i = 0; i < sizey; i++)
                {
                    arY6.Add(pointVy[i]);

                }
                form1.XAxis6 = arX6;
                if (checkBox6.Checked == true)
                    form1.YAxis6 = arY6;

                else
                {
                    arY6.Clear();
                    form1.YAxis6 = arY6;
                }
                

            }
            else
                cb6 = false;
            if (checkBox17.Checked == true)
            {
                cb7 = true;
                arX7.Clear();
                arY7.Clear();

                string[] x = textBox7.Text.Trim().Split();
                string[] y = textBox27.Text.Trim().Split();
                int sizex = x.Length;
                int sizey = y.Length;
                string[] pointVx = new string[sizex];
                string[] pointVy = new string[sizey];
                //float[] pointVy = new float[size];
                for (int i = 0; i < sizex; i++)
                {
                    pointVx[i] = x[i].ToString();
                }
                for (int i = 0; i < sizey; i++)
                {
                    pointVy[i] = y[i].ToString();
                }
                for (int i = 0; i < sizex; i++)
                {
                    arX7.Add(pointVx[i]);
                }
                for (int i = 0; i < sizey; i++)
                {
                    arY7.Add(pointVy[i]);

                }
                form1.XAxis7 = arX7;
                if (checkBox7.Checked == true)
                    form1.YAxis7 = arY7;

                else
                {
                    arY7.Clear();
                    form1.YAxis7 = arY7;
                }
          

            }
            else
                cb7 = false;
            if (checkBox18.Checked == true)
            {
                cb8 = true;
                arX8.Clear();
                arY8.Clear();

                string[] x = textBox8.Text.Trim().Split();
                string[] y = textBox28.Text.Trim().Split();
                int sizex = x.Length;
                int sizey = y.Length;
                string[] pointVx = new string[sizex];
                string[] pointVy = new string[sizey];
                //float[] pointVy = new float[size];
                for (int i = 0; i < sizex; i++)
                {
                    pointVx[i] = x[i].ToString();
                }
                for (int i = 0; i < sizey; i++)
                {
                    pointVy[i] = y[i].ToString();
                }
                for (int i = 0; i < sizex; i++)
                {
                    arX8.Add(pointVx[i]);
                }
                for (int i = 0; i < sizey; i++)
                {
                    arY8.Add(pointVy[i]);

                }
                form1.XAxis8 = arX8;
                if (checkBox8.Checked == true)
                    form1.YAxis8 = arY8;

                else
                {
                    arY8.Clear();
                    form1.YAxis8 = arY8;
                }
                

            }
            else
                cb8 = false;
            if (checkBox19.Checked == true)
            {
                cb9 = true;
                arX9.Clear();
                arY9.Clear();

                string[] x = textBox9.Text.Trim().Split();
                string[] y = textBox29.Text.Trim().Split();
                int sizex = x.Length;
                int sizey = y.Length;
                string[] pointVx = new string[sizex];
                string[] pointVy = new string[sizey];
                //float[] pointVy = new float[size];
                for (int i = 0; i < sizex; i++)
                {
                    pointVx[i] = x[i].ToString();
                }
                for (int i = 0; i < sizey; i++)
                {
                    pointVy[i] = y[i].ToString();
                }
                for (int i = 0; i < sizex; i++)
                {
                    arX9.Add(pointVx[i]);
                }
                for (int i = 0; i < sizey; i++)
                {
                    arY9.Add(pointVy[i]);

                }
                form1.XAxis9 = arX9;
                if (checkBox9.Checked == true)
                    form1.YAxis9 = arY9;

                else
                {
                    arY9.Clear();
                    form1.YAxis9 = arY9;
                }
             

            }
            else
                cb9 = false;
            if (checkBox20.Checked == true)
            {
                cb10 = true;
                arX10.Clear();
                arY10.Clear();

                string[] x = textBox10.Text.Trim().Split();
                string[] y = textBox30.Text.Trim().Split();
                int sizex = x.Length;
                int sizey = y.Length;
                string[] pointVx = new string[sizex];
                string[] pointVy = new string[sizey];
                //float[] pointVy = new float[size];
                for (int i = 0; i < sizex; i++)
                {
                    pointVx[i] = x[i].ToString();
                }
                for (int i = 0; i < sizey; i++)
                {
                    pointVy[i] = y[i].ToString();
                }
                for (int i = 0; i < sizex; i++)
                {
                    arX10.Add(pointVx[i]);
                }
                for (int i = 0; i < sizey; i++)
                {
                    arY10.Add(pointVy[i]);

                }
                form1.XAxis10 = arX10;
                if (checkBox10.Checked == true)
                    form1.YAxis10 = arY10;

                else
                {
                    arY10.Clear();
                    form1.YAxis10 = arY10;
                }
         

            }
            else
                cb10 = false;
            if (form1.toolStripButton1.Text == "关闭")
            {
                form1.time = false;
                form1.toolStripButton7.Text = "停止发送";
                form1.toolStripButton1.Enabled = false;
            }
            
            StreamWriter sw = new StreamWriter(@"TXT.txt");
            sw.WriteLine(textBox1.Text + "\n" + textBox2.Text + "\n" + textBox3.Text + "\n" + textBox4.Text + "\n" + textBox5.Text + "\n" + textBox6.Text + "\n" + textBox6.Text + "\n" + textBox8.Text + "\n" + textBox9.Text + "\n" + textBox10.Text);
            sw.Flush();
            sw.Close();
            form1.Focus();

            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text == "")
                {
                    textBox21.Text = " ";
                }
                else
                {
                    string t;
                    int q;

                    string s = textBox1.Text;
                    int f = textBox1.Text.Length;
                    s = s.Replace(" ", "");
                    string[] c = new string[s.Length];
                    if (s.Length / 2 != 0)
                        q = s.Length - 1;
                    else
                        q = s.Length;
                    for (int i = 2, j = 0, k = 0; j < q; j += 2, k++)
                    {
                        c[k] = s.Substring(j, i);//将s的字符串每两个字节赋值给string数组的一个内存空间
                    }
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        c[i] = Convert.ToString(Convert.ToInt32(c[i], 16));//把c中的每个元素，转化为16进制
                    }

                    byte[] by = new byte[c.Length / 2];
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        by[i] = Convert.ToByte(c[i]);//将c转化为字符数组by               
                    }
                    UInt16 CRC = 0xFFFF, temp = 0xA001;
                    for (int k = 0; k < by.Length; k++)
                    {
                        CRC ^= by[k];
                        for (int i = 0; i < 8; i++)
                        {
                            int j = CRC & 1;
                            CRC >>= 1;
                            CRC &= 0x7FFF;
                            if (j == 1)
                                CRC ^= temp;
                        }
                    }
                    t = Convert.ToString(CRC, 16);

                    if (t.Length == 4)
                    {
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox21.Text = u.ToUpper();
                    }
                    else if (t.Length == 3)
                    {
                        t = "0" + t;
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox21.Text = u.ToUpper();
                    }
                }
            }
            catch
            {
                //MessageBox.Show("请输入正确的数值");
            }

            //textBox2.Text = u.ToUpper();
            u = "";
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (textBox2.Text == "")
                {
                    textBox22.Text = " ";
                }
                else
                {
                    string t;
                    int q;

                    string s = textBox2.Text;
                    int f = textBox2.Text.Length;
                    s = s.Replace(" ", "");
                    string[] c = new string[s.Length];
                    if (s.Length / 2 != 0)
                        q = s.Length - 1;
                    else
                        q = s.Length;
                    for (int i = 2, j = 0, k = 0; j < q; j += 2, k++)
                    {
                        c[k] = s.Substring(j, i);//将s的字符串每两个字节赋值给string数组的一个内存空间
                    }
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        c[i] = Convert.ToString(Convert.ToInt32(c[i], 16));//把c中的每个元素，转化为16进制
                    }

                    byte[] by = new byte[c.Length / 2];
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        by[i] = Convert.ToByte(c[i]);//将c转化为字符数组by               
                    }
                    UInt16 CRC = 0xFFFF, temp = 0xA001;
                    for (int k = 0; k < by.Length; k++)
                    {
                        CRC ^= by[k];
                        for (int i = 0; i < 8; i++)
                        {
                            int j = CRC & 1;
                            CRC >>= 1;
                            CRC &= 0x7FFF;
                            if (j == 1)
                                CRC ^= temp;
                        }
                    }
                    t = Convert.ToString(CRC, 16);

                    if (t.Length == 4)
                    {
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox22.Text = u.ToUpper();
                    }
                    else if (t.Length == 3)
                    {
                        t = "0" + t;
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox22.Text = u.ToUpper();
                    }
                }
            }
            catch
            {
                //MessageBox.Show("请输入正确的数值");
            }

            //textBox2.Text = u.ToUpper();
            u = "";
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
             try
            {
                if (textBox3.Text == "")
                {
                    textBox23.Text = " ";
                }
                else
                {
                    string t;
                    int q;

                    string s = textBox3.Text;
                    int f = textBox3.Text.Length;
                    s = s.Replace(" ", "");
                    string[] c = new string[s.Length];
                    if (s.Length / 2 != 0)
                        q = s.Length - 1;
                    else
                        q = s.Length;
                    for (int i = 2, j = 0, k = 0; j < q; j += 2, k++)
                    {
                        c[k] = s.Substring(j, i);//将s的字符串每两个字节赋值给string数组的一个内存空间
                    }
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        c[i] = Convert.ToString(Convert.ToInt32(c[i], 16));//把c中的每个元素，转化为16进制
                    }

                    byte[] by = new byte[c.Length / 2];
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        by[i] = Convert.ToByte(c[i]);//将c转化为字符数组by               
                    }
                    UInt16 CRC = 0xFFFF, temp = 0xA001;
                    for (int k = 0; k < by.Length; k++)
                    {
                        CRC ^= by[k];
                        for (int i = 0; i < 8; i++)
                        {
                            int j = CRC & 1;
                            CRC >>= 1;
                            CRC &= 0x7FFF;
                            if (j == 1)
                                CRC ^= temp;
                        }
                    }
                    t = Convert.ToString(CRC, 16);


                    if (t.Length == 4)
                    {
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox23.Text = u.ToUpper();
                    }
                    else if (t.Length == 3)
                    {
                        t = "0" + t;
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox23.Text = u.ToUpper();
                    }
                }
            }
            catch
            {
                //MessageBox.Show("请输入正确的数值");
            }

            //textBox2.Text = u.ToUpper();
            u = "";
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
             try
            {
                if (textBox4.Text == "")
                {
                    textBox24.Text = " ";
                }
                else
                {
                    string t;
                    int q;

                    string s = textBox4.Text;
                    int f = textBox4.Text.Length;
                    s = s.Replace(" ", "");
                    string[] c = new string[s.Length];
                    if (s.Length / 2 != 0)
                        q = s.Length - 1;
                    else
                        q = s.Length;
                    for (int i = 2, j = 0, k = 0; j < q; j += 2, k++)
                    {
                        c[k] = s.Substring(j, i);//将s的字符串每两个字节赋值给string数组的一个内存空间
                    }
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        c[i] = Convert.ToString(Convert.ToInt32(c[i], 16));//把c中的每个元素，转化为16进制
                    }

                    byte[] by = new byte[c.Length / 2];
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        by[i] = Convert.ToByte(c[i]);//将c转化为字符数组by               
                    }
                    UInt16 CRC = 0xFFFF, temp = 0xA001;
                    for (int k = 0; k < by.Length; k++)
                    {
                        CRC ^= by[k];
                        for (int i = 0; i < 8; i++)
                        {
                            int j = CRC & 1;
                            CRC >>= 1;
                            CRC &= 0x7FFF;
                            if (j == 1)
                                CRC ^= temp;
                        }
                    }
                    t = Convert.ToString(CRC, 16);


                    if (t.Length == 4)
                    {
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox24.Text = u.ToUpper();
                    }
                    else if (t.Length == 3)
                    {
                        t = "0" + t;
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox24.Text = u.ToUpper();
                    }
                }
            }
            catch
            {
                //MessageBox.Show("请输入正确的数值");
            }

            //textBox2.Text = u.ToUpper();
            u = "";
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
             try
            {
                if (textBox5.Text == "")
                {
                    textBox25.Text = " ";
                }
                else
                {
                    string t;
                    int q;

                    string s = textBox5.Text;
                    int f = textBox5.Text.Length;
                    s = s.Replace(" ", "");
                    string[] c = new string[s.Length];
                    if (s.Length / 2 != 0)
                        q = s.Length - 1;
                    else
                        q = s.Length;
                    for (int i = 2, j = 0, k = 0; j < q; j += 2, k++)
                    {
                        c[k] = s.Substring(j, i);//将s的字符串每两个字节赋值给string数组的一个内存空间
                    }
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        c[i] = Convert.ToString(Convert.ToInt32(c[i], 16));//把c中的每个元素，转化为16进制
                    }

                    byte[] by = new byte[c.Length / 2];
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        by[i] = Convert.ToByte(c[i]);//将c转化为字符数组by               
                    }
                    UInt16 CRC = 0xFFFF, temp = 0xA001;
                    for (int k = 0; k < by.Length; k++)
                    {
                        CRC ^= by[k];
                        for (int i = 0; i < 8; i++)
                        {
                            int j = CRC & 1;
                            CRC >>= 1;
                            CRC &= 0x7FFF;
                            if (j == 1)
                                CRC ^= temp;
                        }
                    }
                    t = Convert.ToString(CRC, 16);


                    if (t.Length == 4)
                    {
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox25.Text = u.ToUpper();
                    }
                    else if (t.Length == 3)
                    {
                        t = "0" + t;
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox25.Text = u.ToUpper();
                    }
                }
            }
            catch
            {
                //MessageBox.Show("请输入正确的数值");
            }

            //textBox2.Text = u.ToUpper();
            u = "";
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
             try
            {
                if (textBox6.Text == "")
                {
                    textBox26.Text = " ";
                }
                else
                {
                    string t;
                    int q;

                    string s = textBox6.Text;
                    int f = textBox6.Text.Length;
                    s = s.Replace(" ", "");
                    string[] c = new string[s.Length];
                    if (s.Length / 2 != 0)
                        q = s.Length - 1;
                    else
                        q = s.Length;
                    for (int i = 2, j = 0, k = 0; j < q; j += 2, k++)
                    {
                        c[k] = s.Substring(j, i);//将s的字符串每两个字节赋值给string数组的一个内存空间
                    }
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        c[i] = Convert.ToString(Convert.ToInt32(c[i], 16));//把c中的每个元素，转化为16进制
                    }

                    byte[] by = new byte[c.Length / 2];
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        by[i] = Convert.ToByte(c[i]);//将c转化为字符数组by               
                    }
                    UInt16 CRC = 0xFFFF, temp = 0xA001;
                    for (int k = 0; k < by.Length; k++)
                    {
                        CRC ^= by[k];
                        for (int i = 0; i < 8; i++)
                        {
                            int j = CRC & 1;
                            CRC >>= 1;
                            CRC &= 0x7FFF;
                            if (j == 1)
                                CRC ^= temp;
                        }
                    }
                    t = Convert.ToString(CRC, 16);


                    if (t.Length == 4)
                    {
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox26.Text = u.ToUpper();
                    }
                    else if (t.Length == 3)
                    {
                        t = "0" + t;
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox26.Text = u.ToUpper();
                    }
                }
            }
            catch
            {
                //MessageBox.Show("请输入正确的数值");
            }

            //textBox2.Text = u.ToUpper();
            u = "";
        
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
             try
            {
                if (textBox7.Text == "")
                {
                    textBox27.Text = " ";
                }
                else
                {
                    string t;
                    int q;

                    string s = textBox7.Text;
                    int f = textBox7.Text.Length;
                    s = s.Replace(" ", "");
                    string[] c = new string[s.Length];
                    if (s.Length / 2 != 0)
                        q = s.Length - 1;
                    else
                        q = s.Length;
                    for (int i = 2, j = 0, k = 0; j < q; j += 2, k++)
                    {
                        c[k] = s.Substring(j, i);//将s的字符串每两个字节赋值给string数组的一个内存空间
                    }
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        c[i] = Convert.ToString(Convert.ToInt32(c[i], 16));//把c中的每个元素，转化为16进制
                    }

                    byte[] by = new byte[c.Length / 2];
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        by[i] = Convert.ToByte(c[i]);//将c转化为字符数组by               
                    }
                    UInt16 CRC = 0xFFFF, temp = 0xA001;
                    for (int k = 0; k < by.Length; k++)
                    {
                        CRC ^= by[k];
                        for (int i = 0; i < 8; i++)
                        {
                            int j = CRC & 1;
                            CRC >>= 1;
                            CRC &= 0x7FFF;
                            if (j == 1)
                                CRC ^= temp;
                        }
                    }
                    t = Convert.ToString(CRC, 16);


                    if (t.Length == 4)
                    {
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox27.Text = u.ToUpper();
                    }
                    else if (t.Length == 3)
                    {
                        t = "0" + t;
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox27.Text = u.ToUpper();
                    }
                }
            }
            catch
            {
                //MessageBox.Show("请输入正确的数值");
            }

            //textBox2.Text = u.ToUpper();
            u = "";
        
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {
             try
            {
                if (textBox8.Text == "")
                {
                    textBox28.Text = " ";
                }
                else
                {
                    string t;
                    int q;

                    string s = textBox8.Text;
                    int f = textBox8.Text.Length;
                    s = s.Replace(" ", "");
                    string[] c = new string[s.Length];
                    if (s.Length / 2 != 0)
                        q = s.Length - 1;
                    else
                        q = s.Length;
                    for (int i = 2, j = 0, k = 0; j < q; j += 2, k++)
                    {
                        c[k] = s.Substring(j, i);//将s的字符串每两个字节赋值给string数组的一个内存空间
                    }
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        c[i] = Convert.ToString(Convert.ToInt32(c[i], 16));//把c中的每个元素，转化为16进制
                    }

                    byte[] by = new byte[c.Length / 2];
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        by[i] = Convert.ToByte(c[i]);//将c转化为字符数组by               
                    }
                    UInt16 CRC = 0xFFFF, temp = 0xA001;
                    for (int k = 0; k < by.Length; k++)
                    {
                        CRC ^= by[k];
                        for (int i = 0; i < 8; i++)
                        {
                            int j = CRC & 1;
                            CRC >>= 1;
                            CRC &= 0x7FFF;
                            if (j == 1)
                                CRC ^= temp;
                        }
                    }
                    t = Convert.ToString(CRC, 16);


                    if (t.Length == 4)
                    {
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox28.Text = u.ToUpper();
                    }
                    else if (t.Length == 3)
                    {
                        t = "0" + t;
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox28.Text = u.ToUpper();
                    }
                }
            }
            catch
            {
                //MessageBox.Show("请输入正确的数值");
            }

            //textBox2.Text = u.ToUpper();
            u = "";
        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {
             try
            {
                if (textBox9.Text == "")
                {
                    textBox29.Text = " ";
                }
                else
                {
                    string t;
                    int q;

                    string s = textBox9.Text;
                    int f = textBox9.Text.Length;
                    s = s.Replace(" ", "");
                    string[] c = new string[s.Length];
                    if (s.Length / 2 != 0)
                        q = s.Length - 1;
                    else
                        q = s.Length;
                    for (int i = 2, j = 0, k = 0; j < q; j += 2, k++)
                    {
                        c[k] = s.Substring(j, i);//将s的字符串每两个字节赋值给string数组的一个内存空间
                    }
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        c[i] = Convert.ToString(Convert.ToInt32(c[i], 16));//把c中的每个元素，转化为16进制
                    }

                    byte[] by = new byte[c.Length / 2];
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        by[i] = Convert.ToByte(c[i]);//将c转化为字符数组by               
                    }
                    UInt16 CRC = 0xFFFF, temp = 0xA001;
                    for (int k = 0; k < by.Length; k++)
                    {
                        CRC ^= by[k];
                        for (int i = 0; i < 8; i++)
                        {
                            int j = CRC & 1;
                            CRC >>= 1;
                            CRC &= 0x7FFF;
                            if (j == 1)
                                CRC ^= temp;
                        }
                    }
                    t = Convert.ToString(CRC, 16);


                    if (t.Length == 4)
                    {
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox29.Text = u.ToUpper();
                    }
                    else if (t.Length == 3)
                    {
                        t = "0" + t;
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox29.Text = u.ToUpper();
                    }
                }
            }
            catch
            {
                //MessageBox.Show("请输入正确的数值");
            }

            //textBox2.Text = u.ToUpper();
            u = "";
        
        
        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {
             try
            {
                if (textBox10.Text == "")
                {
                    textBox30.Text = " ";
                }
                else
                {
                    string t;
                    int q;

                    string s = textBox10.Text;
                    int f = textBox10.Text.Length;
                    s = s.Replace(" ", "");
                    string[] c = new string[s.Length];
                    if (s.Length / 2 != 0)
                        q = s.Length - 1;
                    else
                        q = s.Length;
                    for (int i = 2, j = 0, k = 0; j < q; j += 2, k++)
                    {
                        c[k] = s.Substring(j, i);//将s的字符串每两个字节赋值给string数组的一个内存空间
                    }
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        c[i] = Convert.ToString(Convert.ToInt32(c[i], 16));//把c中的每个元素，转化为16进制
                    }

                    byte[] by = new byte[c.Length / 2];
                    for (int i = 0; i < c.Length / 2; i++)
                    {
                        by[i] = Convert.ToByte(c[i]);//将c转化为字符数组by               
                    }
                    UInt16 CRC = 0xFFFF, temp = 0xA001;
                    for (int k = 0; k < by.Length; k++)
                    {
                        CRC ^= by[k];
                        for (int i = 0; i < 8; i++)
                        {
                            int j = CRC & 1;
                            CRC >>= 1;
                            CRC &= 0x7FFF;
                            if (j == 1)
                                CRC ^= temp;
                        }
                    }
                    t = Convert.ToString(CRC, 16);


                    if (t.Length == 4)
                    {
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox30.Text = u.ToUpper();
                    }
                    else if (t.Length == 3)
                    {
                        t = "0" + t;
                        string[] n = new string[2];
                        for (int i = 2, j = 0, k = 0; j <= n.Length; j += 2, k++)
                        {
                            n[k] = t.Substring(j, i);
                        }
                        Array.Reverse(n);

                        for (int i = 0; i < n.Length; i++)
                        {
                            u += n[i];
                        }
                        int int_len = s.Length;
                        byte[] bytes = new byte[int_len];
                        for (int i = 0; i < int_len; i++)
                        {
                            bytes[i] = (byte)(s[i]);
                        }

                        u = u.Insert(2, " ");
                        textBox30.Text = u.ToUpper();
                    }
                }
            }
            catch
            {
                //MessageBox.Show("请输入正确的数值");
            }

            //textBox2.Text = u.ToUpper();
            u = "";
        
        }
        int len1 = 0;
        
        

       

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void XunHuan_Load(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(@"TXT.txt", FileMode.Open, FileAccess.Read);
            StreamReader m_streamReader = new StreamReader(fs);
            //使用StreamReader类来读取文件 
            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            // 从数据流中读取每一行，直到文件的最后一行，并在txtShow中显示出内容 
            //string strLine = m_streamReader.ReadLine();
            string[] Str = new string[11];
            int i=0;
            while (i<10)
            {
                string strLine;
                //textBox1.Text += strLine +" ";//以逗号隔开
                
                strLine = m_streamReader.ReadLine();
                Str[i] = strLine;
                i++;
            }
            textBox1.Text = Str[0];
            textBox2.Text = Str[1];
            textBox3.Text = Str[2];
            textBox4.Text = Str[3];
            textBox5.Text = Str[4];
            textBox6.Text = Str[5];
            textBox7.Text = Str[6];
            textBox8.Text = Str[7];
            textBox9.Text = Str[8];
            textBox10.Text = Str[9];

            //关闭此StreamReader对象 
            m_streamReader.Close(); 

        }

        private void button4_Click(object sender, EventArgs e)
        {
            StreamWriter sw = new StreamWriter(@"TXT.txt");
            sw.WriteLine(textBox1.Text + "\n" + textBox2.Text + "\n" + textBox3.Text + "\n" + textBox4.Text + "\n" + textBox5.Text + "\n" + textBox6.Text + "\n" + textBox6.Text + "\n" + textBox8.Text + "\n" + textBox9.Text + "\n" + textBox10.Text);
            sw.Flush();
            sw.Close();

        }

        private void XunHuan_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void checkBox21_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox21.Checked == true)
            {
                checkBox11.Checked = true;
                checkBox12.Checked = true;
                checkBox13.Checked = true;
                checkBox14.Checked = true;
                checkBox15.Checked = true;
                checkBox16.Checked = true;
                checkBox17.Checked = true;
                checkBox18.Checked = true;
                checkBox19.Checked = true;
                checkBox20.Checked = true;

            }
            else
            {
                checkBox11.Checked = false;
                checkBox12.Checked = false;
                checkBox13.Checked = false;
                checkBox14.Checked = false;
                checkBox15.Checked = false;
                checkBox16.Checked = false;
                checkBox17.Checked = false;
                checkBox18.Checked = false;
                checkBox19.Checked = false;
                checkBox20.Checked = false;
            }
        }

        private void checkBox22_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox22.Checked == true)
            {
                checkBox1.Checked = true;
                checkBox2.Checked = true;
                checkBox3.Checked = true;
                checkBox4.Checked = true;
                checkBox5.Checked = true;
                checkBox6.Checked = true;
                checkBox7.Checked = true;
                checkBox8.Checked = true;
                checkBox9.Checked = true;
                checkBox10.Checked = true;

            }
            else
            {
                checkBox1.Checked = false;
                checkBox2.Checked = false;
                checkBox3.Checked = false;
                checkBox4.Checked = false;
                checkBox5.Checked = false;
                checkBox6.Checked = false;
                checkBox7.Checked = false;
                checkBox8.Checked = false;
                checkBox9.Checked = false;
                checkBox10.Checked = false;
            }
        }

        private void XunHuan_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }

        private void textBox12_TextChanged(object sender, EventArgs e)
        {
            try
            {
                form1.timer2.Interval = Convert.ToInt32(textBox12.Text);
            }
            catch { }
        }

        private void textBox13_TextChanged(object sender, EventArgs e)
        {
            try
            {
                form1.timer3.Interval = Convert.ToInt32(textBox13.Text);
            }
            catch { }
        }

        private void textBox14_TextChanged(object sender, EventArgs e)
        {
            try
            {
                form1.timer4.Interval = Convert.ToInt32(textBox14.Text);
            }
            catch { }
        }

        private void textBox15_TextChanged(object sender, EventArgs e)
        {
            try
            {
                form1.timer5.Interval = Convert.ToInt32(textBox15.Text);
            }
            catch { }
        }

        private void textBox16_TextChanged(object sender, EventArgs e)
        {
            try
            {
                form1.timer6.Interval = Convert.ToInt32(textBox16.Text);
            }
            catch { }
        }

        private void textBox17_TextChanged(object sender, EventArgs e)
        {
            try
            {
                form1.timer7.Interval = Convert.ToInt32(textBox17.Text);
            }
            catch { }
        }

        private void textBox18_TextChanged(object sender, EventArgs e)
        {
            try
            {
                form1.timer8.Interval = Convert.ToInt32(textBox18.Text);
            }
            catch { }
        }

        private void textBox19_TextChanged(object sender, EventArgs e)
        {
            try
            {
                form1.timer9.Interval = Convert.ToInt32(textBox19.Text);
            }
            catch { }
        }

        private void textBox20_TextChanged(object sender, EventArgs e)
        {
            try
            {
                form1.timer10.Interval = Convert.ToInt32(textBox20.Text);
            }
            catch { }
        }
        
        
        
        
    }
}